/**
 * 
 */
package bai6.com.util;

/**
 * @author nglehien
 * Oct 21, 2012 - 9:04:26 PM
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public final class FileUtil {

	private FileUtil() {

	}

	public static StringBuilder readFile(File file) throws IOException {
		FileInputStream fileInputStream = new FileInputStream(file);
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(fileInputStream, "UTF-8"));
		String data = null;
		StringBuilder myString = new StringBuilder();
		while ((data = bufferedReader.readLine()) != null) {
			myString.append(data + "\n");
		}
		bufferedReader.close();
		return myString;
	}

	public static File writeFile(String data) throws IOException {
		File file = File.createTempFile("temp", "tmp");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		BufferedWriter bufferedWriter = new BufferedWriter(
				new OutputStreamWriter(fileOutputStream, "UTF-8"));
		bufferedWriter.write(data);
		bufferedWriter.newLine();
		bufferedWriter.close();
		return file;
	}

	public static boolean createNewFile(String data, String dst)
			throws IOException {
		try {
			File file = new File(dst);
			FileOutputStream fileOutputStream = new FileOutputStream(file);
			BufferedWriter bufferedWriter = new BufferedWriter(
					new OutputStreamWriter(fileOutputStream, "UTF-8"));
			bufferedWriter.write(data);
			bufferedWriter.close();
			return true;
		} catch (IOException e) {
			return false;
		}
	}
}
